package com.ldsb.moudle.gather;

import com.github.llyb120.tools.ctrl.GroovyMakerController;
import com.ldsb.moudle.gather.parser.ParseContext;
import gather.customerRule.BaseFnExpress;
import gather.customerRule.BaseVfExpress;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class DefaultExpression {
    public BaseFnExpress parser;
    public BaseVfExpress validater;

    public Map evaluate(Context context){
        if(context.getType() == 0){
//            Method[] methods = parser.getClass().getDeclaredMethods();
//            Map<String, Method> methodMap = new HashMap<>();
//            for (Method method : methods) {
//                method
//            }
            LinkedHashMap result = new LinkedHashMap();
            ParseContext parseContext = new ParseContext();
            parseContext.put("erWeiMaInfo", context.values);
            for (GroovyMakerController.Item item : context.request.getList()) {
                String name = item.getName();
                Method method;
                try {
                    method = parser.getClass().getDeclaredMethod(name, ParseContext.class);
                    Object val = method.invoke(parser, parseContext);
                    result.put(name, val);
                } catch (NoSuchMethodException e) {
                    //找不到放到这里面
                    try {
                        method = parser.getClass().getDeclaredMethod("otherAttribute", String.class, ParseContext.class);
                        Object val = method.invoke(parser, item.getName(), parseContext);
                        result.put(name, val);
                    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException noSuchMethodException) {
                        noSuchMethodException.printStackTrace();
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
        return null;
//        if(context)
//        System.out.println("fuck");
    }

}
